package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.qbdms.dao.WQbdFlighttestChildDao;
import com.ccp.dev.qbdms.model.WQbdFlighttestChild;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.ccp.dev.workflow.model.ProcessRun;
import com.ccp.dev.workflow.model.bpm.ProcessCmd;
import com.ccp.dev.workflow.service.ProcessRunService;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 飞行试验子 信息操作服务类
 *
 * @author hjx
 * @date 2020-05-07 15:28:26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdFlighttestChildService extends BaseService<WQbdFlighttestChild> {

    @Resource
    private WQbdFlighttestChildDao dao;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<WQbdFlighttestChild> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdFlighttestChild data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    @Resource
    private ProcessRunService processRunService;

    /**
    * 流程处理器方法 用于处理业务数据
    * @param cmd
    */
    public void processHandler(ProcessCmd cmd){
        Map data=cmd.getFormDataMap();
        if(BeanUtils.isNotEmpty(data)){
            JSONObject obj = JSONObject.fromObject(data);
            WQbdFlighttestChild wQbdFlighttestChild = (WQbdFlighttestChild)JSONObject.toBean(obj, WQbdFlighttestChild.class);
            if(StringUtil.isEmpty(cmd.getBusinessKey())){
                String genId=UUIDUtils.getUUIDFor32();
                wQbdFlighttestChild.setId(genId);
                this.add(wQbdFlighttestChild);
            }else{
                wQbdFlighttestChild.setId(cmd.getBusinessKey());
                this.update(wQbdFlighttestChild);
            }
            cmd.setBusinessKey(wQbdFlighttestChild.getId());
        }
    }

    /**
     * 重写getAll方法绑定流程runId
     */
     @Override
     public List<WQbdFlighttestChild> queryList(QueryFilter queryFilter){
        List<WQbdFlighttestChild> wQbdFlighttestChildList = super.queryList(queryFilter);
        List<WQbdFlighttestChild> wQbdFlighttestChilds = new ArrayList<WQbdFlighttestChild>();
        for(WQbdFlighttestChild wQbdFlighttestChild : wQbdFlighttestChildList){
            ProcessRun processRun = processRunService.getByBusinessKey(wQbdFlighttestChild.getId());
            if(BeanUtils.isNotEmpty(processRun)){
                wQbdFlighttestChild.setRunId(processRun.getRunId());
            }
            wQbdFlighttestChilds.add(wQbdFlighttestChild);
        }
        return wQbdFlighttestChilds;
    }

//通过refid获取信息
    public List<WQbdFlighttestChild> getByMainId(String id){
        return dao.getByMainId(id);
    }
    public int delMainId(String refid){
        return dao.delMainId(refid);
    }
}
